/* UFDAO.java
*  Criado em 02/12/2005
*
*/
package br.com.werp.regras.endereco.integracao;

import br.com.werp.regras.geral.integracao.MasterDAO;
import br.com.werp.regras.geral.util.Constantes;
import br.com.werp.regras.geral.util.Util;
import br.com.werp.regras.geral.vos.tabelas.Uf;

import java.sql.Connection;

import java.util.List;
import java.util.ResourceBundle;


/**
 * @author Rafael Carneiro - <a href = "mailto:rafael@portaljava.com">
 * rafael@portaljava.com</a>
 */
public class UFDAO extends MasterDAO {
    private static UFDAO ufDAO;

    private UFDAO(String banco) throws Exception {
        super(banco);
    }

    public static UFDAO getUFDAO() throws Exception {
        if (ufDAO == null) {
            ResourceBundle bundle = ResourceBundle.getBundle(Constantes.CONFIG);
            ufDAO = new UFDAO(bundle.getString(Constantes.BANCO));
        }

        return ufDAO;
    }

    public List pesquisarUF(Uf uf) throws Exception {
        Connection conn = getConexao();

        try {
            String where = "";

            if (!Util.isNullOrEmpty(uf.getUnfd_descricao())) {
                where += ("and unfd_descricao like " +
                aspasLike(uf.getUnfd_descricao()));
                uf.setUnfd_descricao(null);
            }

            return pesquisarVO(conn, uf, where);
        } finally {
            liberaConeccao(conn);
        }
    }

    public List pesquisarUFVOTabela(Uf uf) throws Exception {
        Connection connection = getConexao();

        try {
            return pesquisarVO(connection, uf);
        } finally {
            liberaConeccao(connection);
        }
    }

    public void salvarUF(Uf uf, Object key) throws Exception {
        Connection connection = validarTransacao(key);

        inserirVO(connection, uf, new String[] { "unfd_id" });
    }

    public void atualizarrUF(Uf uf, Object key) throws Exception {
        Connection connection = validarTransacao(key);

        atualizarVO(connection, uf, new String[] { "unfd_id" });
    }

    public void excluirUF(Uf uf, Object key) throws Exception {
        Connection connection = validarTransacao(key);

        excluirVO(connection, uf);
    }
}
